package com.amar.test.testactivityintent;

import com.amar.stdlib.dbmanager.DbManager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class InsertListaActivity extends Activity {
	private final static String TAG = "INSERT_LISTA_ACTIVITY_TEST";
	private              DbManager db;
	private              SQLiteDatabase myDb;
	private              EditText listaEt;
	private              Button insertBt;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lista);
		Intent intent=getIntent();
		listaEt    = (EditText)findViewById(R.id.editTextLista);
//	     String pkg=getPackageName();
//         
//         String s=intent.getStringExtra(pkg+".myString");  //prendiamo i dati
//         int i=intent.getIntExtra(pkg+".myInt", -1);
//         Person p=(Person)intent.getSerializableExtra(pkg+".myPerson");
//                                 
//         
//         TextView tv=(TextView)findViewById(R.id.result);        
//         tv.append("myString:"+s+"\n"); //visualizziamo i dati
//         tv.append("myInt:"+i+"\n");
//         tv.append("myPerson:"+p+"\n");
	}
	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG, "ON_START " + getActivityName());
		DbManager.arrTable.clear();
		DbManager.arrTable.add(0, Lista.LISTA_TABLE_CREATE);
		DbManager.arrTable.add(1, ListaElementi.LISTA_ELEMENTI_TABLE_CREATE);
		Context myContext = getApplicationContext(); 
		db                = new DbManager(myContext);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG,"ON_RESUME " + getActivityName());
		myDb = db.open();
		listaEt.setFocusable(true);
		insertBt = (Button) findViewById(R.id.buttonInsertLista);
		insertBt.setOnClickListener(OnClick);

	}
	
	@Override
	protected void onStop() {
		super.onStop();
		db.close();
		Log.d(TAG,"ON_STOP " + getActivityName());
	}
	
	protected String getActivityName() {
		return "InsertListaActivityY";
	}
	
	 private OnClickListener OnClick=new OnClickListener(){
	       @Override
	       public void onClick(View v) {
	       switch(v.getId()){
	              case R.id.buttonInsertLista:{
	            	  long keyLista_id = Lista.insertLista(myDb, listaEt.getText().toString());
	                  if(Lista.IdFetchLista(myDb,keyLista_id).getCount()==1){
	                     Toast.makeText(getApplicationContext(), "Lista Inserita",Toast.LENGTH_LONG).show();
//	                     displayListView();
	                     listaEt.setText("");
	                     break;}
	              }
//	              case R.id.buttonFindLista:{
//	            	    FindDisplayListView(ricercaEt.getText().toString());
//	            	    ricercaEt.setText("");
//	                    break;}
//	              }
	        }

		 };
     };
}

